﻿using System;
using System.ComponentModel;

namespace NanoMvvm.ComponentModel
{
    internal class StaticTypeDescriptor : CustomTypeDescriptor
    {
        private readonly WeakReference<TypeViewModel> targetInstance;

        private StaticTypeDescriptor(TypeViewModel tvm)
        {
            targetInstance = new WeakReference<TypeViewModel>(tvm);
        }

        public override PropertyDescriptorCollection GetProperties()
        {
            if (targetInstance.IsTargetAlive)
            {
                return new PropertyDescriptorCollection(targetInstance.Target.GetWrappedProperties());
            }

            return new PropertyDescriptorCollection(new PropertyDescriptor[] { });
        }

        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            if (targetInstance.IsTargetAlive)
            {
                return new PropertyDescriptorCollection(targetInstance.Target.GetWrappedProperties(attributes));
            }

            return new PropertyDescriptorCollection(new PropertyDescriptor[] { });
        }

        internal static StaticTypeDescriptor GetDescriptor(TypeViewModel tvm)
        {
            return new StaticTypeDescriptor(tvm);
        }
    }
}